#include "MouseManager.h"

bool MouseManager::Init(HINSTANCE hinstance, HWND hwnd)
{
	HRESULT hr;

	// Create the DirectInput object.
	hr=DirectInput8Create( hinstance,	
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		( void** )&m_DirectInputObject,
		NULL );

	//create mouse device
	hr=m_DirectInputObject->CreateDevice(GUID_SysMouse, &m_MouseDevice, NULL);

	//set data format
	hr=m_MouseDevice->SetDataFormat(&c_dfDIMouse);

	//set cooperative level
	hr=m_MouseDevice->SetCooperativeLevel(hwnd,DISCL_FOREGROUND);

	hr=m_MouseDevice->Acquire();

	if(FAILED(hr))
		return false;
	else
		return true;
}

MouseManager::~MouseManager()
{
	if(m_DirectInputObject)
	{
		m_DirectInputObject->Release();
		m_DirectInputObject=NULL;
	}
	if(m_MouseDevice)
	{
		m_MouseDevice->Unacquire();
		m_MouseDevice->Release();
		m_MouseDevice=NULL;
	}
}

void MouseManager::Update()
{
	if(DIERR_INPUTLOST == m_MouseDevice->GetDeviceState(sizeof(m_MouseState),
		(LPVOID)&m_MouseState))
	{
		m_MouseDevice->Acquire();
	}
	else
	{

		m_CurrentCoordinates.x+=static_cast<float>(m_MouseState.lX);
		m_CurrentCoordinates.y+=static_cast<float>(m_MouseState.lY);
		m_CurrentCoordinates.z+=static_cast<float>(m_MouseState.lZ);
	}


}

D3DXVECTOR3 MouseManager::GetMousePosition()
{
	return m_CurrentCoordinates;
}

D3DXVECTOR3 MouseManager::GetMouseMovement()
{
	D3DXVECTOR3 distance;
	distance.x=static_cast<float>(m_MouseState.lX);
	distance.y=static_cast<float>(m_MouseState.lY);
	distance.z=static_cast<float>(m_MouseState.lZ);

	return distance;
}

bool MouseManager::MouseButtonDown(int button)
{
	if(m_MouseState.rgbButtons[button] & 0x80)
	{
		return true;
	}

	return false;

}